package com.example.xmldemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initView();
        try {
            List<PersonModel> result=XMLUtil.parasePerson(getAssets().open("person_list.xml"));
            for (PersonModel person:result) {
                Log.d(TAG,"id="+person.getId()+" name="+person.getName()+" age="+person.getAge());
            }

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void initView(){
        try {
            //获取xml数据流
            InputStream inputStream=getAssets().open("person_list.xml");
            //创建SAX解析工具类
            MyHandle handle=new MyHandle();
            //获取SAX解析器
            SAXParser parser= SAXParserFactory.newInstance().newSAXParser();
            //开始解析
            parser.parse(inputStream,handle);
            inputStream.close();
            List<PersonModel> result=handle.getList();
            for (PersonModel person:result) {
                Log.d(TAG,"id="+person.getId()+" name="+person.getName()+" age="+person.getAge());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
    }
}